package com.jinke.uang.ui.activity.personal;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jinke.uang.BuildConfig;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.bean.LoginData;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.NoDoubleClickListener;
import com.jinke.uang.common.UserInfo;
import com.jinke.uang.http.ParamsFactory;
import com.jinke.uang.http.RetrofitManager;
import com.jinke.uang.rxbus.EventMsg;
import com.jinke.uang.rxbus.Events;
import com.jinke.uang.rxbus.RxBus;
import com.jinke.uang.utils.StringUtils;
import com.jinke.uang.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 老用户登录界面
 */
@SuppressLint("CheckResult")
@Route(name = "短信登录页", path = RouterMap.LOGIN_SMS)
public class LoginSmsActivity extends AppCompatActivity {

    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.etTextPhone)
    EditText etTextPhone;
    @BindView(R.id.flSmsCode)
    FrameLayout flSmsCode;
    @BindView(R.id.etSmsCode)
    EditText etSmsCode;
    @BindView(R.id.tvsmsCode)
    TextView tvsmsCode;
    @BindView(R.id.tvBtn)
    TextView tvBtn;

    private int status=1;//1 需要获取短信验证码的  2 可以登录的
    private String tel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_sms);
        ButterKnife.bind(this);
        topBack.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                super.onNoDoubleClick(v);
                finish();
            }
        });
        if(1==status){
            etTextPhone.setVisibility(View.VISIBLE);
            flSmsCode.setVisibility(View.GONE);
        }else{
            etTextPhone.setVisibility(View.GONE);
            flSmsCode.setVisibility(View.VISIBLE);
        }
        tvBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                super.onNoDoubleClick(v);
                if(1==status){
                    //获取短信验证码的
                    getCode();
                }else if(2==status){
                    login();
                }
            }
        });
    }

    /**
     * 获取短信验证码
     */
    void getCode(){
        tel=etTextPhone.getText().toString().trim();
        if(StringUtils.isBlank(tel)){
            ToastUtils.showShort(this,"请输入正确的手机号！");
            return;
        }
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("mobile",tel);
        Observable<BaseResponse<String>> send =
                RetrofitManager.getInstance().getINetService().loginSms(
                        ParamsFactory.getCommonParams(formMap));
        send.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(stringBaseResponse -> {
                       if(200==stringBaseResponse.getStatus()){
                           status=2;
                           etTextPhone.setVisibility(View.GONE);
                           flSmsCode.setVisibility(View.VISIBLE);
                           tvBtn.setText("登录");
                           if(time!=null){
                               time.start();
                           }
                       }else{
                           ToastUtils.showShort(this,stringBaseResponse.getMessage());
                       }
                }, throwable -> {
                    ToastUtils.showShort(this,"获取短信验证码报错的！");
                });
    }

    /**
     * 登录的
     */
    void login(){
        String sms=etSmsCode.getText().toString().trim();
        if(StringUtils.isBlank(sms)){
            ToastUtils.showShort(this,"请输入正确的验证码！");
            return;
        }
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("mobile",tel);
        formMap.put("verificationCode",sms);
        Observable<BaseResponse<LoginData>> send =
                RetrofitManager.getInstance().getINetService().loginSmslogin(BuildConfig.channel,
                        ParamsFactory.getCommonParams(formMap));
        send.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(stringBaseResponse -> {
                       if(200==stringBaseResponse.getStatus()){
                               String token = stringBaseResponse.getData().getToken();
                               UserInfo.setToken(token);
                               EventMsg msg = new EventMsg();
                               msg.setMsg(Events.LOGIN);
                               RxBus.getInstance().post(msg);
                               finish();
                       }else{
                           ToastUtils.showShort(this,stringBaseResponse.getMessage());
                       }
                }, throwable -> {
                    ToastUtils.showShort(this,"获取短信验证码报错的！");
                });
    }


    CountDownTimer time=new CountDownTimer(60000L,1000L) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvsmsCode.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            tvsmsCode.setText("重新获取验证码");
            tvBtn.setText("重新获取验证码");
        }
    };
}



